package light.head.dao.oa;

import java.util.List;

import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.model.InStockItem;

import org.springframework.stereotype.Service;
/***
 * 物品入库明细
 * @author yy
 *
 */
@Service
public class InStockItemDAO extends BaseDAO<InStockItem, Integer>{
	private static final String hql_findByInStockCode = " FROM InStockItem ins WHERE (ins.deleted = 0 OR ins.deleted IS NULL) AND ins.sc = ? ORDER BY ins.code";
	private static final String hql_findAllByInStockCode = " FROM InStockItem ins WHERE ins.sc = ? ORDER BY ins.code";
	
	
	
	
	/**
	 * 获取 入库信息编号insCode 指定的所有入库明细
	 * @param insCode 订单信息编号
	 * @return 返回insCode指定的明细集合
	 */
	public List<InStockItem> findByInStockCode(Integer insCode) {
		if (insCode != null) return find(hql_findByInStockCode, insCode);
		return null;
	}
	
	/**
	 * 获取 入库信息编号insCode 指定的所有入库明细(包括delted = 1 的)
	 * @param insCode
	 */
	public List<InStockItem> findAllByInStockCode(Integer insCode) {
		if (insCode != null) return find(hql_findAllByInStockCode, insCode);
		return null;
	}
	/**
	 * 通过入库信息编号集合查询所有的入库明细
	 * @author YangGuangHe
	 * @Time 2013-11-27 下午3:21:56
	 * @param insCodes 入库信息编号集合
	 */
	public List<InStockItem> findByInStockCodes(List<Object> insCodes) {
		if (null == insCodes || insCodes.size() == 0) return null;
		
		StringBuffer hql = new StringBuffer(" FROM InStockItem isi WHERE (isi.deleted = 0 OR isi.deleted IS NULL) AND isi.sc IN (");
		for (int i = 0; i < insCodes.size(); i++) {
			hql.append("?,");
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		return find(hql.toString(), null, null, insCodes.toArray());
	}
	
	public RESULT update(InStockItem ins) {
		if(ins == null || ins.getCode() == null) return RESULT.ERROR;
		
		InStockItem old = get(ins.getCode());
		
		if(old == null) return RESULT.NOT_FIND;
		
		if(ins.getDeleted() != null && ins.getDeleted() != old.getDeleted())
			old.setDeleted(ins.getDeleted());
		if(ins.getGc() != null && ins.getGc() != old.getGc())
			old.setGc(ins.getGc());
		
		return upd(old);
	}
	
	/**
	 * 根据机构编号oc 和 商品编号gc 获得商品入库明细
	 */
	public List<InStockItem> findBycc(Integer gc){
		String hql = " FROM InStockItem isi WHERE isi.gc = ?";
		return findI(hql,gc);
	}
}
